package com.tmall.wireless.virtualviewdemo.utils.imageloader.core.imageaware;

import com.tmall.wireless.virtualviewdemo.utils.imageloader.core.assist.ViewScaleType;
import com.tmall.wireless.virtualviewdemo.utils.imageloader.utils.L;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.element.Element;
import ohos.media.image.PixelMap;

import java.lang.reflect.Field;

public class ImageViewAware extends ViewAware {


    public ImageViewAware(Image imageView) {
        super(imageView);
    }


    public ImageViewAware(Image imageView, boolean checkActualViewSize) {
        super(imageView, checkActualViewSize);
    }

    /**
     * {@inheritDoc}
     * <br />
     * 3) Get <b>maxWidth</b>.
     *
     * @return int
     */
    @Override
    public int getWidth() {
        int width = super.getWidth();
        if (width <= 0) {
            Image imageView = (Image) viewRef.get();
            if (imageView != null) {
                ComponentContainer.LayoutConfig layoutConfig = imageView.getLayoutConfig();
                if (layoutConfig != null) {
                    int width1 = layoutConfig.width;
                    return width1 >= 0 ? width1 : 0;
                }
                // width = getImageViewFieldValue(imageView, "mMaxWidth"); // Check maxWidth parameter
            }
        }
        return width;
    }

    /**
     * {@inheritDoc}
     * <br />
     * 3) Get <b>maxHeight</b>
     *
     * @return int
     */
    @Override
    public int getHeight() {
        int height = super.getHeight();
        if (height <= 0) {
            Image imageView = (Image) viewRef.get();

            if (imageView != null) {
                ComponentContainer.LayoutConfig layoutConfig = imageView.getLayoutConfig();
                if (layoutConfig != null) {
                    int height1 = layoutConfig.height;
                    return height1 >= 0 ? height1 : 0;
                }
                // height = getImageViewFieldValue(imageView, "mMaxHeight"); // Check maxHeight parameter
            }
        }
        return height;
    }

    @Override
    protected void setImageElementInto(Element element, Component view) {
        ((Image) view).setImageElement(element);
    }

    @Override
    protected void setImagePixelMapInto(PixelMap pixelMap, Component view) {
        ((Image) view).setPixelMap(pixelMap);
    }

    @Override
    public ViewScaleType getScaleType() {
        Image imageView = (Image) viewRef.get();
        if (imageView != null) {
            return ViewScaleType.fromImageView(imageView);
        }
        return super.getScaleType();
    }

    @Override
    public Image getWrappedView() {
        return (Image) super.getWrappedView();
    }


//    private static int getImageViewFieldValue(Object object, String fieldName) {
//        int value = 0;
//        try {
//            Field field = Image.class.getDeclaredField(fieldName);
//            field.setAccessible(true);
//            int fieldValue = (Integer) field.get(object);
//            if (fieldValue > 0 && fieldValue < Integer.MAX_VALUE) {
//                value = fieldValue;
//            }
//        } catch (Exception e) {
//            L.e(e);
//        }
//        return value;
//    }
}
